package com.questionnaires.utils;

//import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Contains useful methods for managing users.
 *
 * @author ioana.ruca@gmail.com
 */
public final class UserUtils {

  private UserUtils() {
  }

  /**
   * Encodes a string using MD5.
   *
   * @param text the string to be encoded
   *
   * @return the encoded string
   */
  public static String encode(String text) {
    try {
      MessageDigest m = MessageDigest.getInstance("MD5");
      m.update(text.getBytes("UTF8"));
      byte[] s = m.digest();
      String result = "";
      for (int i = 0; i < s.length; i++) {
        result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
      }
      return result;
    } catch (NoSuchAlgorithmException ex) {
//      LOG.error("Could not find encoding algorithm!", ex);
    } catch (UnsupportedEncodingException ex) {
//      LOG.error("Could not encode string!", ex);
    }
    return null;
  }
}
